﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CE.iPhone.PList;
namespace cczEdit
{
    public partial class UCUnitAnimSetting : UserControl
    {
        PListRoot root;
        List<Hero> heros;
        public UCUnitAnimSetting()
        {
            InitializeComponent();
            heros = new List<Hero>();
            LoadHeroData();

            foreach (Hero hero in heros)
            {
                comboBoxName.Items.Add(hero.name);
            }
            root = PListRoot.Load(Form1.cczDirector + "\\SAnimData.plist");
            foreach (KeyValuePair<string, IPListElement> kv in (PListDict)root.Root)
            {
                Hero hero = heros.Find((h) => { return h.num == Int32.Parse(kv.Key); });
                listBox1.Items.Add(hero.name);
            }
            if (listBox1.Items.Count > 0)
                listBox1.SelectedIndex = 0;
            
        }
        void LoadHeroData()
        {
            root = PListRoot.Load(Form1.cczDirector + "\\HeroData.plist");
            PListDict rootDic = (PListDict)root.Root;
            PListDict herosDic = (PListDict)rootDic["Heros"];
            foreach (KeyValuePair<String, IPListElement> a in herosDic)
            {
                PListDict heroDic = (PListDict)a.Value;
                PListInteger num = (PListInteger)heroDic["Num"];
                PListString name = (PListString)heroDic["Name"];
                PListInteger numHead = (PListInteger)heroDic["NumHead"];
                PListInteger numMsgCrit = (PListInteger)heroDic["NumMsgCrit"];
                PListInteger numMsgDeath = (PListInteger)heroDic["NumMsgDeath"];
                PListInteger numRAnim = (PListInteger)heroDic["NumRAnim"];
                PListInteger numSAnim = (PListInteger)heroDic["NumSAnim"];
                PListInteger numTeam = (PListInteger)heroDic["NumTeam"];
                PListString dspt = (PListString)heroDic["Dspt"];
                PListInteger agile = (PListInteger)heroDic["Agile"];
                PListInteger command = (PListInteger)heroDic["Command"];
                PListInteger force = (PListInteger)heroDic["Force"];
                PListInteger intelligence = (PListInteger)heroDic["Intelligence"];
                PListInteger luck = (PListInteger)heroDic["Luck"];
                PListInteger HP = (PListInteger)heroDic["HP"];
                PListInteger MP = (PListInteger)heroDic["MP"];
                Hero hero = new Hero();
                hero.name = name.Value;
                hero.num = num.Value;
                hero.numHead = numHead.Value;
                hero.numMsgCrit = numMsgCrit.Value;
                hero.numMsgDeath = numMsgDeath.Value;
                hero.numRAnim = numRAnim.Value;
                hero.numSAnim = numSAnim.Value;
                hero.numTeam = numTeam.Value;
                hero.dspt = dspt.Value;
                hero.agile = agile.Value;
                hero.command = command.Value;
                hero.force = force.Value;
                hero.intelligence = intelligence.Value;
                hero.luck = luck.Value;
                hero.hp = HP.Value;
                hero.mp = MP.Value;
                heros.Add(hero);

            }


        }
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            Hero hero = heros.Find((h) => { return h.name == comboBoxName.Text; });
            if (hero == null)
                MessageBox.Show("不存在此武将，请在\"武将资料\"界面添加");
            PListDict dict = (PListDict)root.Root;
            PListDict sAnimSetting = new PListDict();
            dict.Add(""+hero.num,sAnimSetting);
            sAnimSetting["SAnim1"] = new PListInteger((long)numericUpDown1.Value);
            sAnimSetting["SAnim2"] = new PListInteger((long)numericUpDown2.Value);
            sAnimSetting["SAnim3"] = new PListInteger((long)numericUpDown3.Value);
            listBox1.Items.Add(hero.name);
            listBox1.SelectedIndex = listBox1.Items.Count - 1;
        }
       
        private void buttonDel_Click(object sender, EventArgs e)
        {
            if (listBox1.Items.Count == 0||listBox1.SelectedItem==null)
            {
                return;
            }
            Hero hero = heros.Find((h) => { return h.name == listBox1.Text; });
            PListDict dict = (PListDict)root.Root;
            dict.Remove("" + hero.num);
            listBox1.Items.RemoveAt(listBox1.SelectedIndex);
            if(listBox1.Items.Count>0)
                listBox1.SelectedIndex = 0;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            Hero hero = heros.Find((h) => { return h.name == comboBoxName.Text; });
            if (hero == null)
                MessageBox.Show("不存在此武将，请在\"武将资料\"界面添加");
            PListDict dict = (PListDict)root.Root;
            if (dict.Keys.Contains(hero.num + ""))
            {
                PListDict sAnimSetting = (PListDict)dict["" + hero.num];
                sAnimSetting["SAnim1"] = new PListInteger((long)numericUpDown1.Value);
                sAnimSetting["SAnim2"] = new PListInteger((long)numericUpDown2.Value);
                sAnimSetting["SAnim3"] = new PListInteger((long)numericUpDown3.Value);
                listBox1.Items.RemoveAt(listBox1.SelectedIndex);
                listBox1.Items.Add(hero.name);
                listBox1.SelectedIndex = listBox1.Items.Count - 1;
            }
            
            root.Save(Form1.cczDirector + "\\SAnimData.plist");
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Hero hero = heros.Find((h) => { return h.name == listBox1.Text; });
            if (hero == null)
                return;
            PListDict dict = (PListDict)root.Root;
            PListDict sAnimSetting = (PListDict)dict["" + hero.num];
            comboBoxName.Text = hero.name;
            PListInteger sAnim1 = (PListInteger)sAnimSetting["SAnim1"];
            PListInteger sAnim2 = (PListInteger)sAnimSetting["SAnim2"];
            PListInteger sAnim3 = (PListInteger)sAnimSetting["SAnim3"];
            numericUpDown1.Value = sAnim1.Value;
            numericUpDown2.Value = sAnim2.Value;
            numericUpDown3.Value = sAnim3.Value;
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            pictureBox1.Image = Image.FromFile(Form1.cczDirector + "\\UnitSPC\\UnitSPC" + numericUpDown1.Value + ".png");

        }

        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {
            pictureBox2.Image = Image.FromFile(Form1.cczDirector + "\\UnitSPC\\UnitSPC" + numericUpDown2.Value + ".png");

        }

        private void numericUpDown3_ValueChanged(object sender, EventArgs e)
        {
            pictureBox3.Image = Image.FromFile(Form1.cczDirector + "\\UnitSPC\\UnitSPC" + numericUpDown3.Value + ".png");

        }

    }
}
